package resources

import (
	"cloud_android_backend/pkg/models/products"
	"github.com/jinzhu/gorm"
	"time"
)

//CREATE TABLE `instances` (
//  `id` bigint NOT NULL AUTO_INCREMENT,
//  `machine_id` bigint DEFAULT NULL,
//  `mac` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `ip` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `product_id` int DEFAULT NULL,
//  `package_id` int DEFAULT NULL,
//  `created_at` datetime DEFAULT NULL,
//  `updated_at` datetime DEFAULT NULL,
//  `status` tinyint DEFAULT NULL COMMENT '0 未出售 1已出售 2已销毁',
//`adb_address` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `height` int DEFAULT NULL,
//  `width` int DEFAULT NULL,
//  `image` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `image_tag` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `index` int DEFAULT NULL,
// `cpu_cores` bigint DEFAULT NULL,
//  `memory` bigint DEFAULT NULL,
//  `storage` float DEFAULT NULL,
//`state` tinyint(1) DEFAULT NULL COMMENT '0离线 1在线',
//`name` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//`fps` int NOT NULL DEFAULT '30',
//  `dpi` int NOT NULL DEFAULT '400',
//  `is_realphone` tinyint(1) NOT NULL DEFAULT '0',
//  `image_id` int DEFAULT NULL,
//  `macvlan_name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'my_bridge',
//	`adi_template_id` int DEFAULT NULL,
//`cat_id` int NOT NULL DEFAULT '1',
//`adb_input_installed` tinyint(1) NOT NULL DEFAULT '0',
// PRIMARY KEY (`id`)
//) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

type Instances struct {
	Id                int64             `json:"id" gorm:"primary_key;column:id"`
	MachineId         int64             `json:"machine_id" gorm:"column:machine_id"`
	Mac               string            `json:"mac" gorm:"column:mac"`
	Ip                string            `json:"ip" gorm:"column:ip"`
	ProductId         int               `json:"product_id" gorm:"column:product_id"`
	PackageId         int               `json:"package_id" gorm:"column:package_id"`
	CreatedAt         time.Time         `json:"created_at" gorm:"column:created_at"`
	UpdatedAt         time.Time         `json:"updated_at" gorm:"column:updated_at"`
	Status            int               `json:"status" gorm:"column:status"`
	AdbPort           int               `json:"adb_port" gorm:"column:adb_port"`
	AdbAddress        string            `json:"adb_address" gorm:"column:adb_address"`
	Height            int               `json:"height" gorm:"column:height"`
	Width             int               `json:"width" gorm:"column:width"`
	Image             string            `json:"image" gorm:"column:image"`
	ImageTag          string            `json:"image_tag" gorm:"column:image_tag"`
	Index             int               `json:"index" gorm:"column:index"`
	State             int               `json:"state" gorm:"column:state"`
	CpuCores          int               `json:"cpu_cores" gorm:"column:cpu_cores"`
	Memory            int               `json:"memory" gorm:"column:memory"`
	Storage           float64           `json:"storage" gorm:"column:storage"`
	Name              string            `json:"name" gorm:"column:name"`
	Machine           Machines          `json:"machine" gorm:"foreignkey:MachineId"`
	Product           products.Products `json:"product" gorm:"foreignkey:ProductId"`
	Fps               int               `json:"fps" gorm:"column:fps"`
	Dpi               int               `json:"dpi" gorm:"column:dpi"`
	IsRealPhone       int               `json:"is_realphone" gorm:"column:is_realphone"`
	ImageId           int               `json:"image_id" gorm:"column:image_id"`
	MacVlanName       string            `json:"macvlan_name" gorm:"column:macvlan_name"`
	AdiTemplateId     int64             `json:"adi_template_id" gorm:"column:adi_template_id"`
	AdbInputInstalled int               `json:"adb_input_installed" gorm:"column:adb_input_installed"`
	CatId             int               `json:"cat_id" gorm:"column:cat_id"`
}

const (
	InstanceStatus_NotSold = 0
	InstanceStatus_Sold    = 1
	InstanceStatus_Destroy = 2
)

func (a *Instances) TableName() string {
	return "instances"
}
func (a *Instances) Create(db *gorm.DB) error {
	return db.Table(a.TableName()).Create(a).Error
}
func (a *Instances) Update(db *gorm.DB) error {
	return db.Table(a.TableName()).Where("id = ?", a.Id).Update(a).Error
}
func (a *Instances) UpdatePartFields(db *gorm.DB, fields map[string]interface{}) error {
	return db.Table(a.TableName()).Where("id = ?", a.Id).Update(fields).Error
}
func (a *Instances) GetById(db *gorm.DB) error {
	return db.Table(a.TableName()).Where("id = ?", a.Id).First(a).Error
}
func (a *Instances) GetList(db *gorm.DB, offset int, limit int, search map[string]interface{}) ([]Instances, int, error) {
	var list []Instances
	var total int
	db = db.Table(a.TableName()).Where(search)
	db.Model(&Instances{}).Count(&total)
	db.Offset(offset).Limit(limit).Find(&list)
	return list, total, db.Error
}
func (a *Instances) GetListByMachineId(db *gorm.DB, machineId int64) ([]Instances, error) {
	var list []Instances
	db.Table(a.TableName()).Where("machine_id = ?", machineId).Find(&list)
	return list, db.Error
}
func (a *Instances) SoftDelete(db *gorm.DB, id int64) error {
	return db.Table(a.TableName()).Where("id = ?", id).Update(&Instances{Status: InstanceStatus_Destroy}).Error
}

func (a *Instances) CountByProductId(db *gorm.DB) (int, error) {
	var count int
	db.Table(a.TableName()).Where("product_id = ?", a.ProductId).Count(&count)
	return count, db.Error
}

func (a *Instances) CountUnusedByCatId(db *gorm.DB) (int, error) {
	var count int
	db.Table(a.TableName()).Where("status = ?", InstanceStatus_NotSold).Where("cat_id = ?", a.CatId).Count(&count)
	return count, db.Error
}
func (a *Instances) GetByProductIdAndStatusForUpdate(db *gorm.DB, productId, status int) ([]Instances, error) {
	var list []Instances
	db.Table(a.TableName()).Set("gorm:query_option", "FOR UPDATE").Where("status = ?", status).Where("product_id = ?", productId).Find(&list)
	return list, db.Error
}

func (a *Instances) GetByCatIdAndStatusForUpdate(db *gorm.DB, CatId, status int) ([]Instances, error) {
	var list []Instances
	db.Table(a.TableName()).Set("gorm:query_option", "FOR UPDATE").Where("status = ?", status).Where("cat_id = ?", CatId).Find(&list)
	return list, db.Error
}
